import { defineStore } from "pinia";
import { AppStoreState } from "../types";
import { Locale } from "nutui-uniapp/locale";
import { nutLocaleMaps } from "@/locale";
import { themeConfig, ThemeConfig } from "@/config/theme";
import createPersist from "../persist";
import { updateObjProps } from "@/utils/modules/data";
const id = "appConfig";
export default defineStore({
  id,
  persist: createPersist(id),
  state: (): AppStoreState => ({
    lang: uni.getLocale(),
    theme: "light",
    themeConfig: themeConfig,
  }),
  getters: {},
  actions: {
    changeLang(lang: string) {
      this.lang = lang;
      uni.setLocale(lang);
      // nutui 多语言切换
      Locale.use("en-US", nutLocaleMaps[lang]());

      /* nutui 语言覆盖
			  Locale.merge({
				confirm: '已确认',
			  })
			*/
    },
    changeTheme() {
      this.theme = this.theme == "light" ? "dark" : "light";
    },
    changeThemeConfig<V>(key: string, value: V) {
      this.themeConfig = updateObjProps(
        this.themeConfig,
        key,
        value
      ) as ThemeConfig;
    },
  },
});
